<?php
    $farmID=$_GET["farmID"];
    require_once dirname(__FILE__).'/../../class/farm.class.php';
    require_once dirname(__FILE__).'/../../class/date.class.php';
    require_once dirname(__FILE__).'/../../class/rice.class.php';
    $riceObj = new rice();
    $rice=$riceObj->search();
   
    $dateObj=new date;
    
    $farmObj=new farm($farmID);
    $farm=$farmObj->getFarm();
    
    if(isset($_GET["cropID"])){
        require_once dirname(__FILE__).'/../../class/farm.class.php';
        $cropID=$_GET["cropID"];
        $cropObj=new crop($cropID);
        $crop=$cropObj->getCrop();
    }
?>
<style type="text/css">
    #structureContent{
        vertical-align: top;
    }
</style>
<div style="padding: 5px;">
    <div class="panel panel-default">
        <div class="panel-heading">
            <span class="glyphicon glyphicon-plus"></span>
            เพิ่มครอป
            <span class="glyphicon glyphicon-remove" style="float: right;cursor: pointer;" onclick="window.open('?page=member/crop/cropList&farmID=<?=$farmID?>','_self')"></span>
        </div>
        <div class="box-inner">
            <form method="post" id="addCropForm" class="form-horizontal" action="member/crop/addCrop.php">
                <input type="hidden" name="cropID" value="<?=$_GET["cropID"]?>">
                <input type="hidden" id="farmID" name="farmID" value="<?=$_GET["farmID"]?>">
                <fieldset>

                    <!-- Text input-->
                    <div class="form-group">
                        <label class="col-md-4 control-label" for="farmName">ข้อมูลแปลง</label>  
                        <div class="col-md-4">
                            <b><?=$farm["farmName"]?></b> พื้นที่ <?=$farm["area"]?> ไร่<br>
                            <?php
                                $address=$farmObj->getFarmAddress();
                                echo $address;
                            ?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-4 control-label" for="farmName">ช่วงเวลาที่ปลูก</label>  
                        <div class="col-md-4">
                            <?php
                                if($crop["startDate"]){
                                    $startDate=$dateObj->dateSQLToDisplat($crop["startDate"]);
                                }
                                
                                if($crop["endDate"]){
                                    $endDate=$dateObj->dateSQLToDisplat($crop["endDate"]);
                                }
                            ?>
                            <input id="startDate" name="startDate" class="form-control input-md" type="text" style="width:auto;float:left;margin-right: 5px;width: 100px"
                                <?php
                                    if($crop["startDate"]){
                                        ?>value="<?=$startDate?>"<?php
                                    }
                                ?>
                                >
                            <div style="margin-top: 5px;margin-left: 15px;margin-right: 15px;float: left;">ถึง</div>
                            <input id="endDate" name="endDate" class="form-control input-md" type="text" style="width:auto;float:left;margin-right: 5px;width: 100px;"
                                <?php
                                    if($crop["endDate"]){
                                        ?>value="<?=$endDate?>"<?php
                                    }
                                ?>
                                >
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label class="col-md-4 control-label" for="farmName">พันธุ์ข้าว</label>  
                        <div class="col-md-4">
                            <select id="rvID" name="rvID" class="form-control">
                                <?php
                                    foreach ($rice as $key => $value) {
                                        ?>
                                <option value="<?=$key?>"
                                    <?php
                                        if(isset($crop)){
                                            if($crop["rvID"]==$key){
                                                echo "selected";
                                            }
                                        }
                                    ?>
                                    ><?=$value["rvName"]?></option>
                                            <?php
                                    }
                                ?>
                            </select>
                        </div>
                    </div>

                    <div id="memberMap">
                        <div id="map-canvas"></div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-4 control-label" for=""></label>
                        <div class="col-md-4">
                            <input class="btn btn-danger" type="button" value="ลบครอป"
                                onclick="
                                    if(confirm('ต้องการลบครอปนี้')){
                                        window.open('member/crop/deleteCrop.php?cropID=<?=$_GET["cropID"]?>','_self');
                                    }
                                "
                                >
                            <button id="" name="" class="btn btn-primary" style="float: right;">
                                <span class="glyphicon glyphicon-floppy-disk"></span> บันทึก
                            </button>
                        </div>
                    </div>
                </fieldset>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function() {
        $( "#startDate" ).datepicker( $.datepicker.regional["th"] ); // Set ภาษาที่เรานิยามไว้ด้านบน
        <?php
            if($crop["startDate"]==""){
                ?>
        $( "#startDate" ).datepicker("setDate", new Date()); //Set ค่าวันปัจจุบัน
                    <?php
            }
        ?>
        
        
        $( "#endDate" ).datepicker( $.datepicker.regional["th"] ); // Set ภาษาที่เรานิยามไว้ด้านบน
        <?php
            if($crop["endDate"]==""){
                ?>
        $( "#endDate" ).datepicker("setDate", new Date()); //Set ค่าวันปัจจุบัน
                    <?php
            }
        ?>
        
    });
</script>